package com.qmyx.supplychain.quartz.task;

import com.github.pagehelper.util.StringUtil;
import com.ytjj.qmyx.supplychain.mapper.entity.YxBankerOrder;
import com.ytjj.qmyx.supplychain.service.ExpressOrderService;
import com.ytjj.qmyx.supplychain.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 异常单扫描定时任务
 */
@Component
@Slf4j
public class OrderExceptionTask {
    @Autowired
    private ExpressOrderService expressOrderService;

    @Autowired
    private ProductService productService;
    /**
     * 查询超1-6天内未发货订单
     */
    @Scheduled(cron = "0 0/30 * * * ?")
    public void checkNoExpressExceptionOrders(){
        List<YxBankerOrder> bankerOrderList = expressOrderService.queryBankerOrderList(1);
        log.info("超时24小时未发货订单数:{}",bankerOrderList.size());
        bankerOrderList.stream().forEach(item -> {
            // 如果快递号为null 加入异常
            if (StringUtil.isEmpty(item.getExpressNo())) {
                expressOrderService.handleExceptionOrderNoExpress(item, "未录入快递单号-已超48小时", -1);
            }
        });
        log.info("超时24小时未发货订单数 is end");
    }


    /**
     * 查询超7-30天未发货订单
     */
    @Scheduled(cron = "0 0 0/1 * * ?")
    public void checkNoExpressExceptionOrdersStep2(){
        List<YxBankerOrder> bankerOrderList = expressOrderService.queryBankerOrderList(2);
        log.info("超时6天未发货订单数:{}",bankerOrderList.size());
        bankerOrderList.stream().forEach(item -> {
            // 如果快递号为null 加入异常
            if (StringUtil.isEmpty(item.getExpressNo())) {
                expressOrderService.handleExceptionOrderNoExpress(item, "未录入快递单号-已超7天", -1);
            }
        });
        log.info("超时6天未发货订单数 is end");
    }

    /**
     * 查询超30天未发货订单（每天10点）
     */
    @Scheduled(cron = "0 0 10 * * ?")
    public void checkNoExpressExceptionOrdersStep3(){
        List<YxBankerOrder> bankerOrderList = expressOrderService.queryBankerOrderList(3);
        log.info("超时6天未发货订单数:{}",bankerOrderList.size());
        bankerOrderList.stream().forEach(item -> {
            // 如果快递号为null 加入异常
            if (StringUtil.isEmpty(item.getExpressNo())) {
                expressOrderService.handleExceptionOrderNoExpress(item, "未录入快递单号-已超30天", -1);
            }
        });
        log.info("超时30天未发货订单数 is end");
    }


    /**
     * 超2小时仍然是冻结中的库存，状态更新为-冻结作废(超时未支付/用户取消订单)
     */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void checkProductStockFrozen(){
        log.info("===============开始查询超2小时仍然是冻结中的库存=================");
        productService.checkProductStockFrozen();
        log.info("===============结束查询超2小时仍然是冻结中的库存=================");
    }
}
